package com.ljkj.cordial.chat.util;

import android.app.Activity;
import android.content.Intent;
import android.text.Editable;
import android.text.TextUtils;
import android.util.ArrayMap;
import android.widget.EditText;

import com.ljkj.cordial.chat.activity.GroupMemberAtelActivity;
import com.ljkj.cordial.chat.bean.Canstants;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * @成员 整体删除
 * 添加@跳转
 * 删除@或删除带成员的@无影响
 */
public class AtUtil {
    private static class Build {
        public static AtUtil INSTANCE = new AtUtil();
    }

    public static AtUtil getInstance() {
        return Build.INSTANCE;
    }

    private static String space = " ";
    private List<String> atList = new ArrayList<>();
    private ArrayMap<String, String> userMap = new ArrayMap<>();
    private int lastLength = -1;

    public List<String> getNickNames() {
        List<String> strings = new ArrayList<>();
        if (!atList.isEmpty()) {
            for (String s : atList) {
                strings.add(s.trim().substring(1));
            }
        }
        return strings;
    }

    public ArrayMap<String, String> getUserMap() {
        return userMap;
    }

    public void afterTextChanged(EditText editText, String groupId) {
        Editable e = editText.getText();
        if (TextUtils.isEmpty(e.toString())) {
            clear();
            return;
        }
        if (lastLength > e.length()) {//-
            int sel = editText.getSelectionStart();
            if (!atList.isEmpty()) {
                Iterator<String> it = atList.iterator();
                while (it.hasNext()) {
                    String entry = it.next();
                    try {
                        int st = sel - (entry.length() - 1);
                        if (st >= 0) {
                            String v = e.toString().substring(st, sel);
                            System.out.println(sel + "," + st + "," + v);
                            if (v.equals(entry.trim())) {
                                it.remove();
                                userMap.remove(entry);
                                e.delete(st, sel);
                            }
                        }
                    } catch (Exception e1) {
                        e1.printStackTrace();
                    }
                }
            }
        } else {//+
            int sel = editText.getSelectionStart();
            if (e.toString().lastIndexOf("@") == sel - 1) {
                GroupMemberAtelActivity.jumpTo((Activity) editText.getContext(), groupId);
            }
        }
        lastLength = e.length();
        containNickName(e.toString());
    }

    private void containNickName(String content) {
        if (TextUtils.isEmpty(content)) {
            clear();
            return;
        }
        if (!atList.isEmpty()) {
            Iterator<String> it = atList.iterator();
            while (it.hasNext()) {
                String entry = it.next();
                try {
                    if (!content.contains(entry)) {
                        it.remove();
                        userMap.remove(entry);
                    }
                } catch (Exception e1) {
                    e1.printStackTrace();
                }
            }
        }
    }

    public void onActivityResult(EditText etText, int requestCode, int resultCode, Intent data) {
        if (requestCode == Canstants.REQUEST_CODE_300 && resultCode == Canstants.RESULT_CODE_300 && data != null) {
            String nickname = data.getStringExtra(Canstants.KEY_NICKNAME);
            String userId = data.getStringExtra(Canstants.KEY_MEMBER_ID);
            insertAtNickname(etText, nickname, userId, 1);
            return;
        }
    }

    public void insertAt(EditText etText, String nickname, String userId) {
        insertAtNickname(etText, nickname, userId, 0);
    }

    private void insertAtNickname(EditText etText, String nickname, String userId, int beginSubIndex) {
        String value = "@" + nickname + space;
        userMap.put(value, userId);
        int key = etText.getSelectionStart();
        if (!atList.contains(value)) {
            atList.add(value);
            etText.getText().insert(key, value.substring(beginSubIndex));
        } else {
            if (beginSubIndex > 0)
                etText.getText().delete(key - 1, key);
        }
    }

    public void clear() {
        atList.clear();
        userMap.clear();
        lastLength = 0;
    }
}
